package com.luyuan.framework.security.authentication;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 多用户认证管理器
 * 支持多种用户类型的认证
 * 
 * @author luyuan
 */
@Component
public class MultiUserAuthenticationManager {
    
    @Autowired
    private UserDetailsService userDetailsService;
    
    @Autowired
    private UserDetailsService wxappUserDetailsService;
    
    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 获取网站用户认证管理器
     */
    public AuthenticationManager getWebsiteAuthenticationManager() {
        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
        daoAuthenticationProvider.setUserDetailsService(userDetailsService);
        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);
        List<AuthenticationProvider> providers = new ArrayList<>();
        providers.add(daoAuthenticationProvider);
        return new ProviderManager(providers);
    }
    
    /**
     * 获取微信小程序用户认证管理器
     */
    public AuthenticationManager getWxappAuthenticationManager() {
        // 微信小程序用户不需要密码认证，使用自定义的认证提供者
        WxappAuthenticationProvider wxappAuthenticationProvider = new WxappAuthenticationProvider();
        wxappAuthenticationProvider.setUserDetailsService(wxappUserDetailsService);
        List<AuthenticationProvider> providers = new ArrayList<>();
        providers.add(wxappAuthenticationProvider);
        return new ProviderManager(providers);
    }
}